package com.yang.frost.blueberry.service.impl;

import com.yang.frost.blueberry.domain.SysPermission;
import com.yang.frost.blueberry.mapper.SysPermissionMapper;
import com.yang.frost.blueberry.mapper.SysRolePermissionMapper;
import com.yang.frost.blueberry.service.SysRolePermissionService;
import org.apache.commons.collections4.SetUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 10296
 * @date 2021/1/25
 * @Description:
 */
@Service
public class SysRolePermissionServiceImpl implements SysRolePermissionService {

    private Logger logger = LoggerFactory.getLogger(SysLoginLogServiceImpl.class);
    @Autowired
    private SysRolePermissionMapper rolePermissionMapper;
    @Autowired
    private SysPermissionMapper sysPermissionMapper;

    @Override
    public int saveSysRolePermission(Long roleId, List<Long> permissionIds) throws Exception {
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("roleId", roleId);
        paramsMap.put("permissionIds", permissionIds);

        return rolePermissionMapper.saveSysRolePermission(paramsMap);
    }

    @Override
    public List<Long> getPermissionIdsByRoleId(Long roleId) throws Exception {
        return null;
    }

    @Override
    public List<Long> getThreeLevelPermissionIdsByRoleId(Long roleId) throws Exception {
        return null;
    }

    @Override
    public boolean saveSysRolePermissionBatch(Long roleId, SetUtils.SetView addSet) {
        return false;
    }

    @Override
    public boolean deleteSysRolePermissionByRoleId(Long roleId) throws Exception {
        return false;
    }

    @Override
    public Set<String> getPermissionCodesByRoleId(Long roleId) throws Exception {
        return rolePermissionMapper.getPermissionCodesByRoleId(roleId);
    }

    @Override
    public boolean isExistsByPermissionId(Long permissionId) throws Exception {
        return false;
    }

    @Override
    public boolean hasPermission(Long roleId) throws Exception {
        return false;
    }

    @Override
    public List<SysPermission> getMenuTreeByUserId(String userId) {
        List<SysPermission> sysPermissions = sysPermissionMapper.qrySysPermMenuByUserId(userId);
        List<SysPermission> resultList = getChildPermission(sysPermissions, 0);

        return resultList;
    }


    private List<SysPermission> getChildPermission(List<SysPermission> sysPermissions, int parentId) {
        List<SysPermission> resultList = new ArrayList<>();

        for (SysPermission t : sysPermissions) {
            // 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
            if (t.getParentId() == parentId) {
                recursionFn(sysPermissions, t);
                resultList.add(t);
            }


        }


        return resultList;
    }

    /**
     * 递归列表
     *
     * @param list
     * @param t
     */
    private void recursionFn(List<SysPermission> list, SysPermission t) {
        // 得到子节点列表
        List<SysPermission> childList = getChildList(list, t);
        t.setChildren(childList);
        for (SysPermission tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<SysPermission> getChildList(List<SysPermission> list, SysPermission t) {
        List<SysPermission> tlist = new ArrayList<>();
        for (SysPermission n : list) {
            if (n.getParentId().longValue() == t.getMenuId().longValue()) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<SysPermission> list, SysPermission t) {
        return getChildList(list, t).size() > 0;
    }


}
